package com.itzg.vboxfarm.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.itzg.vboxfarm.domain.HostMachine;
import com.itzg.vboxfarm.domain.Site;
import com.itzg.vboxfarm.service.HostConfigService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	Logger logger = LoggerFactory.getLogger(AdminController.class);
	
	private HostConfigService hostConfigService;

	@Autowired
	public AdminController(HostConfigService hostConfigService) {
		this.hostConfigService = hostConfigService;
	}

	@RequestMapping(value="/sites", method=RequestMethod.GET)
	public String getSites(Model model) {

		model.addAttribute("siteList", hostConfigService.getAllSites());
		return "admin/sites";
	}
	
	@RequestMapping(value="/sites/add", method=RequestMethod.GET)
	public String prepAdd(Model model) {
		logger.info("prepAdd");
		Site site = new Site();
		site.setName("Site-"+System.currentTimeMillis()/1000);
		model.addAttribute(site);
		return "admin/site-add";
	}
	
	@RequestMapping(value="/sites/add", method=RequestMethod.POST)
	public String doAdd(Site site) {
		logger.info("Adding site {}", site);
		hostConfigService.addSite(site);
		return "redirect:/admin/sites";
	}
	
	@RequestMapping(value="/sites/{siteId}/hosts", method=RequestMethod.GET)
	public String getHosts(@PathVariable long siteId, Model model) {
		Site site = hostConfigService.findSite(siteId);
		if (site == null) {
			return "redirect:/admin/sites";
		}
		model.addAttribute("site", site);
		model.addAttribute("hosts", hostConfigService.getHostsForSite(site));
		return "admin/hosts";
	}
	
	@RequestMapping(value="/sites/{siteId}/addHost", method=RequestMethod.GET)
	public String prepAddHost(@PathVariable long siteId, Model model) {
		Site site = hostConfigService.findSite(siteId);
		if (site == null) {
			return "redirect:/admin/sites";
		}
		model.addAttribute("site", site);
		model.addAttribute("host", new HostMachine());
		return "admin/host-add";
	}
	
	@RequestMapping(value="/sites/{siteId}/addHost", method=RequestMethod.POST)
	public String doAddHost(@PathVariable long siteId, HostMachine host) {
		Site site = hostConfigService.findSite(siteId);
		if (site == null) {
			return "redirect:/admin/sites";
		}
		host.setSite(site);
		hostConfigService.addHost(host);
		return "redirect:/admin/sites/{siteId}/hosts";
	}

	@RequestMapping(value="/sites/{siteId}/checkHost/{hostId}", method=RequestMethod.GET)
	public String doCheckHostFromSite(@PathVariable long siteId, @PathVariable long hostId, Model model) {
		Site site = hostConfigService.findSite(siteId);
		if (site == null) {
			return "redirect:/admin/sites";
		}
		model.addAttribute("site", site);
		hostConfigService.checkHost(hostId);
		return "redirect:/admin/sites/{siteId}/hosts";
	}
	
	@RequestMapping(value="/hosts/{hostId}", method={RequestMethod.GET})
	public String prepGetHost(@PathVariable long hostId, Model model) {
		HostMachine host = hostConfigService.findHost(hostId);
		if (host != null) {
			model.addAttribute("host", host);
			return "admin/host";
		}
		else {
			logger.warn("Invalid host {}", hostId);
			return "redirect:/admin/sites";
		}
	}
	
	@RequestMapping(value="/hosts/{hostId}/check", method=RequestMethod.GET)
	public String doCheckHost(@PathVariable long hostId, Model model) {
		hostConfigService.checkHost(hostId);
		return "redirect:/admin/hosts/{hostId}";
	}
	
	@RequestMapping(value="/hosts/{hostId}", method={RequestMethod.DELETE})
	public String doRemoveHost(@PathVariable long hostId) {
		HostMachine host = hostConfigService.removeHost(hostId);
		if (host != null) {
			return "redirect:/admin/sites/" + host.getSite().getId() + "/hosts";
		}
		else {
			return "redirect:/admin/sites";
		}
		
	}
}
